<?php
/**
 * 后台客户，化妆品品牌系列的管理
 *
 * Author: hzz
 * Date: 2018-9-4 18:11:16
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Customer\Product;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Customer\BrandSeries\BrandSeriesStoreRequest;
use App\Http\Requests\Admin\Customer\BrandSeries\BrandSeriesUpdateRequest;
use App\Models\Panx\Brand;
use App\Models\Panx\BrandSeries;
use App\Services\Utils\Guard;
use App\Services\Utils\RequestParam;
use Illuminate\Http\Request;

class BrandSeriesController extends ApiController
{

    const ROUTE_NAMES = [
        'index'   => 'api.admin.customer.banner-series.index',
        'store'   => 'api.admin.customer.banner-series.store',
        'update'  => 'api.admin.customer.banner-series.update',
        'destroy' => 'api.admin.customer.banner-series.destroy',
    ];

    /**
     * @test
     *
     * @param Request $request
     * @return array|\Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $brand_series = (new BrandSeries)
            ->where('customer_id', Guard::customerAdmin()->customer_id)
            ->with('brand')
            ->oldest('sort')
            ->paginate($request->per_page ?: 15);

        return $this->response(compact('brand_series'));
    }

    /**
     * @return array|\Illuminate\Http\Response
     */
    public function create()
    {
        $brands = (new Brand)
            ->where('customer_id', Guard::customerAdmin()->customer_id)
            ->latest()
            ->get(['name', 'id']);

        return $this->response(compact('brands'));
    }

    /**
     * @test
     * @param BrandSeriesStoreRequest $request
     * @return array|\Illuminate\Http\Response
     */
    public function store(BrandSeriesStoreRequest $request)
    {
        $customer_id = Guard::customerAdmin()->customer_id;

        $store_data = RequestParam::normalize(
            array_keys($request->rules()),
            ['customer_id'=>$customer_id]
        );

        $store = (new BrandSeries)->create($store_data);

        return $this->response(compact('store'));

    }

    /**
     * @test
     * @param $id
     * @param BrandSeriesUpdateRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function update($id, BrandSeriesUpdateRequest $request)
    {
        $customer_id = Guard::customerAdmin()->customer_id;
        $brand_series = (new BrandSeries)->where('customer_id', $customer_id)
            ->find($id);
            $this->checkItemExist($brand_series, trans('admin.brand_series'));

        $update_data = RequestParam::normalize(
            array_keys($request->rules())
        );

        $update = $brand_series->update($update_data);
        return $this->response(compact('update'));
    }

    /**
     * @test
     * @param $id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy($id)
    {
        $customer_id = Guard::customerAdmin()->customer_id;
        $brand_series = (new BrandSeries)->where('customer_id', $customer_id)
            ->find($id);
            $this->checkItemExist($brand_series, trans('admin.brand_series'));
            $this->checkUnless($brand_series->products()->exists(), trans('admin.brand_series_used'));

        $delete = $brand_series->delete();
        return $this->response(compact('delete'));
    }
}
